import type { IFormListData } from '@/services/listTable';

export async function dealColumnListPower(configData: IFormListData, k: string) {
  const { baseConfig } = configData;
  const x = {};

  const cacheColumnListMap = new Map(Object.entries(x));
  // 权限参数，如果col 有就取col ,没有则取baseConfig, pov 只取baseConfig 中的
  return (configData[k] || []).map((item: any) => {
    const newItem = item;
    const curCacheItem = cacheColumnListMap.get(item.id) as { cacheCookiesActive?: boolean };
    const { rsName, rsMapping, rsElementDetail, roleScript, rsDimensionElementDetail } = item;
    if (item.valueType === 8) {
      newItem.valueTypeMap.rsName = rsElementDetail
        ? rsName
        : item.isRole
        ? baseConfig.rsName
        : null;
      newItem.valueTypeMap.roleScript = rsElementDetail
        ? roleScript
        : !item.isRole
        ? roleScript
        : configRoleScript(baseConfig);
      newItem.valueTypeMap.rsMapping = rsElementDetail
        ? rsMapping
        : item.isRole
        ? baseConfig.colAliasRoleDim?.[item.alias]?.rsMapping
        : null;
      newItem.valueTypeMap.rsElementDetail =
        rsElementDetail || (item.isRole ? baseConfig.rsElementDetail : null);
      newItem.valueTypeMap.rsDimensionElementDetail = rsElementDetail
        ? rsDimensionElementDetail
        : item.isRole
        ? {
            ...baseConfig.colAliasRoleDim?.[item.alias],
            folderId: baseConfig.colAliasRoleDim?.[item.alias]?.rsDimensionFolderId,
          }
        : null;
    }
    newItem.valueTypeMap.thousandthSymbol = newItem.thousandthSymbol;
    newItem.cookiesActive = curCacheItem?.cacheCookiesActive ?? item.isActive;
    return newItem;
  });
}

// 将baseConfig 中 权限数据参数处理成标准 roleScript 格式，用于公共组件
function configRoleScript(baseConfig: IBaseConfig) {
  return {
    allRoles: baseConfig?.rsRole === '-1',
    groupInfo: (baseConfig?.rsRoleGroup || '').split(',').map((name: string) => {
      return {
        name,
      };
    }),
    roleInfo: (baseConfig?.rsRole || '').split(',').map((name: string) => {
      return {
        name,
      };
    }),
  };
}

interface IBaseConfig {
  rsRole?: string;
  rsRoleGroup?: string;
}
